package com.at.window11;

import com.at.bean.WaterSensor;
import com.at.functions5.WaterSensorMapFunction3;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/9/26 21:07
 */
public class WindowProcessDemo4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> sensorDS = env
                .socketTextStream("localhost", 7777)
                .map(new WaterSensorMapFunction3());

        KeyedStream<WaterSensor, String> sensorKS = sensorDS.keyBy(WaterSensor::getId);

        // TODO 1. 指定 窗口分配器： 指定 用 哪一种窗口 --- 时间 or 计数？ 滚动、滑动、会话？
        WindowedStream<WaterSensor, String, TimeWindow> sensorWS = sensorKS
                .window(TumblingProcessingTimeWindows.of(Time.seconds(10)));// 滚动窗口，窗口长度为10s

        SingleOutputStreamOperator<String> process = sensorWS
//                .apply(new WindowFunction<WaterSensor, String, String, TimeWindow>() {
//                    private static final long serialVersionUID = -4432583168212045036L;
//
//                    /**
//                     *
//                     * @param s 分组的key
//                     * @param window 窗口对象
//                     * @param input 存的数据
//                     * @param out 采集器
//                     * @throws Exception
//                     */
//                    @Override
//                    public void apply(String s, TimeWindow window, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
//                    }
//                })
                .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>() {
                    private static final long serialVersionUID = 7286108888028619740L;

                    /**
                     *
                     * @param s 分组的key
                     * @param context 上下文
                     * @param elements 存的数据
                     * @param out 采集器
                     * @throws Exception
                     */
                    @Override
                    public void process(String s, ProcessWindowFunction<WaterSensor, String, String, TimeWindow>.Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                        long start = context.window().getStart();
                        long end = context.window().getEnd();
                        String windowStart = DateFormatUtils.format(start, "yyyy-MM-dd HH:mm:ss.SSS");
                        String windowEnd = DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss.SSS");

                        long count = elements.spliterator().estimateSize();
                        out.collect("key=" + s + "的窗口【" + windowStart + "," + windowEnd + "】,数据条数：" + count + ", ==》" + elements);
                    }
                });
        process.print();

        env.execute();
    }
}
